package at.ac.tuwien.ir.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class FileController implements IFileController 
{
	
	public FileController()
	{
		
	}

	
	@Override
	public List<String> getFolderNames(String folder) 
	{
		File file = new File(folder);
		String[] directories = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name)
			{
				return new File(dir, name).isDirectory();
			}
		});
		
		List<String> dirList = new ArrayList<String>();
		dirList.addAll(Arrays.asList(directories));
		
		return dirList;
	}
	
	@Override
	public List<String> getDocumentNames(String folder)
	{
		File file = new File(folder);
		List<String> fileList = new ArrayList<String>();
		//ignore hidden files on unix systems
		//and I mean you .DS_Store
		String[] documents = file.list(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name)
			{
				return !name.startsWith(".");
			}
		});
		
		fileList.addAll(Arrays.asList(documents));
		
		return fileList;
	}


	@Override
	public StringBuilder getFileContents(String filename) throws FileNotFoundException {
		StringBuilder text = new StringBuilder();
		//@SuppressWarnings("resource")
		Scanner scanner = new Scanner(new FileInputStream(filename));
		while(scanner.hasNextLine()) {
			text.append(scanner.nextLine());
		}
		
		return text;
	}
	

}
